package com.cloud.wms.ui.group.obtain;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.eventBus.Event;
import com.cloud.core.eventBus.EventBusUtil;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.group.contract.GroupObtainContract;
import com.cloud.wms.ui.group.entity.ObtainBindEntity;
import com.cloud.wms.ui.group.presenter.GroupObtainPresenter;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.widget.dialog.AlertDialogBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-03-10 10:24
 * @Description:
 */
public class ObtainBindActivity extends BaseBarListActivity<String, GroupObtainPresenter> implements GroupObtainContract.View {
    @BindView(R.id.btn_confirm)
    TextView btnConfirm;
    @BindView(R.id.ll_btn)
    LinearLayout llBtn;
    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.tv_line_code)
    TextView tvLineCode;
    @BindView(R.id.iv_reset)
    ImageView ivReset;
    @BindView(R.id.ll_line)
    LinearLayout llLine;
    @BindView(R.id.tv_operator_list)
    TextView tvOperatorList;
    private long billId;
    private TaskEntity taskEntity;
    private ObtainBindEntity lineEntity = new ObtainBindEntity();
    private List<ObtainBindEntity> userEntityList = new ArrayList<>();

    @Override
    protected String barTitle() {
        return "领用绑定";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_group_obtain_bind;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_group_obtain_bind;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        taskEntity = getIntent().getParcelableExtra(IntentKey.TASK_KEY);
        billId = getIntent().getLongExtra(IntentKey.ID_KEY, 0);
        initScanText(scanText);
        rvList.setVisibility(View.GONE);
        llBtn.setVisibility(View.GONE);
        tvOperatorList.setVisibility(View.GONE);
        llLine.setVisibility(View.GONE);
        mAdapter.setNewInstance(new ArrayList<>());
        mPresenter.loadBindInfo();
    }

    @Override
    public void onReload() {
        super.onReload();
        mPresenter.loadBindInfo();
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        if (llLine.getVisibility() == View.GONE) {
            mPresenter.checkLineCode(result);
        } else {
            mPresenter.checkOperator(mAdapter.getData(), result);
        }
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, String item) {
        helper.setText(R.id.tv_job_code, item);
    }

    @Override
    protected void itemClickListener(BaseQuickAdapter<String, BaseViewHolder> mAdapter, View view, int position) {
        super.itemClickListener(mAdapter, view, position);
        new AlertDialogBuilder(this)
                .setTitle("删除确认")
                .setMessage("是否要移除工号：" + mAdapter.getData().get(position) + "?")
                .setPositiveButton("移除", (dialog, which) -> {
                    mAdapter.removeAt(position);
                    userEntityList.remove(position);
                })
                .setNegativeButton("取消", null)
                .show();
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        switch (message.what) {
            case GroupObtainPresenter.BIND_INFO:
                ObtainBindEntity entity = (ObtainBindEntity) message.obj;
                if (TextUtils.isEmpty(entity.getAssemblylineNo()))
                    return;
                tvLineCode.setText(entity.getAssemblylineNo());
                mAdapter.setNewInstance(entity.getEmployeesNo());
                btnConfirm.setEnabled(true);
                llLine.setVisibility(View.VISIBLE);
                llBtn.setVisibility(View.VISIBLE);
                tvOperatorList.setVisibility(View.VISIBLE);
                rvList.setVisibility(View.VISIBLE);
                scanText.setHint("操作工号");
                for (int i = 0; i < entity.getEmployeesID().size(); i++) {
                    ObtainBindEntity obtainBindEntity = new ObtainBindEntity();
                    obtainBindEntity.setEmployeeID(entity.getEmployeesID().get(i));
                    obtainBindEntity.setEmployeeCode(entity.getEmployeesNo().get(i));
                    userEntityList.add(obtainBindEntity);
                }
                lineEntity.setAssemblylineNo(entity.getAssemblylineNo());
                lineEntity.setAssemblylineID(entity.getAssemblylineID());
                break;
            case GroupObtainPresenter.CHECK_LINE:
                success("流水线号扫描成功！");
                lineEntity = (ObtainBindEntity) message.obj;
                llLine.setVisibility(View.VISIBLE);
                tvLineCode.setText(lineEntity.getAssemblylineNo());
                scanText.setHint("操作工号");
                scanText.getFocus();
                break;
            case GroupObtainPresenter.CHECK_OPERATOR:
                success("操作工号扫描成功！");
                ObtainBindEntity userEntity = (ObtainBindEntity) message.obj;
                mAdapter.addData(0, userEntity.getEmployeeCode());
                userEntityList.add(0, userEntity);
                btnConfirm.setEnabled(true);
                llBtn.setVisibility(View.VISIBLE);
                tvOperatorList.setVisibility(View.VISIBLE);
                rvList.setVisibility(View.VISIBLE);
                break;
            case GroupObtainPresenter.SUBMIT_OBTAIN:
                success("团购任务领用成功！");
                EventBusUtil.sendEvent(new Event<>(EventConfig.TASK_REFRESH));
                EventBusUtil.sendEvent(new Event<>(EventConfig.CLOSE_REFRESH));
                this.finish();
                break;
        }
    }

    @OnClick({R.id.tv_line_code, R.id.iv_reset, R.id.btn_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_line_code:
            case R.id.iv_reset:
                rvList.setVisibility(View.GONE);
                llBtn.setVisibility(View.GONE);
                tvOperatorList.setVisibility(View.GONE);
                llLine.setVisibility(View.GONE);
                scanText.setHint("流水线号");
                scanText.getFocus();
                mAdapter.getData().clear();
                mAdapter.notifyDataSetChanged();
                success("重置成功，请扫描流水线号！");
                break;
            case R.id.btn_confirm:
                List<String> employeesIDList = new ArrayList<>();
                for (int i = 0; i < userEntityList.size(); i++) {
                    employeesIDList.add(userEntityList.get(i).getEmployeeID());
                }
                mPresenter.confirmObtain(taskEntity, billId, String.valueOf(lineEntity.getAssemblylineID()), employeesIDList, mAdapter.getData());
                break;
        }
    }

    public static void start(Context context, TaskEntity taskEntity, long billId) {
        Intent intent = new Intent(context, ObtainBindActivity.class);
        intent.putExtra(IntentKey.TASK_KEY, taskEntity);
        intent.putExtra(IntentKey.ID_KEY, billId);
        context.startActivity(intent);
    }
}
